/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.shardingsphere.data.pipeline.core.task;

import org.apache.shardingsphere.data.pipeline.core.constant.PipelineSQLOperationType;
import org.apache.shardingsphere.data.pipeline.core.ingest.position.IngestPosition;
import org.apache.shardingsphere.data.pipeline.core.ingest.position.type.finished.IngestFinishedPosition;
import org.apache.shardingsphere.data.pipeline.core.ingest.position.type.placeholder.IngestPlaceholderPosition;
import org.apache.shardingsphere.data.pipeline.core.ingest.record.DataRecord;
import org.apache.shardingsphere.data.pipeline.core.ingest.record.FinishedRecord;
import org.apache.shardingsphere.data.pipeline.core.ingest.record.PlaceholderRecord;
import org.apache.shardingsphere.data.pipeline.core.task.progress.IncrementalTaskProgress;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collections;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.jupiter.api.Assertions.assertNull;

@ExtendWith(MockitoExtension.class)
class IncrementalTaskAckCallbackTest {
    
    @Mock
    private IngestPosition position;
    
    private final IncrementalTaskProgress taskProgress = new IncrementalTaskProgress(position);
    
    private final IncrementalTaskAckCallback callback = new IncrementalTaskAckCallback(taskProgress);
    
    @Test
    void assertOnAckWithIngestPlaceholderPosition() {
        callback.onAck(Collections.singletonList(new PlaceholderRecord(new IngestPlaceholderPosition())));
        assertNull(taskProgress.getPosition());
        assertThat(taskProgress.getIncrementalTaskDelay().getLastEventTimestamps(), is(0L));
        assertThat(taskProgress.getIncrementalTaskDelay().getLatestActiveTimeMillis(), is(0L));
    }
    
    @Test
    void assertOnAckWithFinishedRecord() {
        IngestPosition position = new IngestFinishedPosition();
        FinishedRecord finishedRecord = new FinishedRecord(position);
        finishedRecord.setCommitTime(1L);
        callback.onAck(Collections.singletonList(finishedRecord));
        assertThat(taskProgress.getPosition(), is(position));
        assertThat(taskProgress.getIncrementalTaskDelay().getLastEventTimestamps(), is(1L));
        assertThat(taskProgress.getIncrementalTaskDelay().getLatestActiveTimeMillis(), is(0L));
    }
    
    @Test
    void assertOnAckWithDataRecord() {
        IngestPosition position = new IngestFinishedPosition();
        DataRecord dataRecord = new DataRecord(PipelineSQLOperationType.DELETE, "t_order", position, 3);
        dataRecord.setCommitTime(2L);
        callback.onAck(Collections.singletonList(dataRecord));
        assertThat(taskProgress.getPosition(), is(position));
        assertThat(taskProgress.getIncrementalTaskDelay().getLastEventTimestamps(), is(2L));
        assertThat(System.currentTimeMillis() - taskProgress.getIncrementalTaskDelay().getLatestActiveTimeMillis(), lessThan(10000L));
    }
}
